package com.ms.seguridad.support;

import org.primefaces.component.separator.Separator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.MethodExpressionActionListener;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

//import org.primefaces.model.MenuModel;

/**
 * User: gmc
 * Date: 26/04/13
 */

public class RolMenuModel implements  Serializable {

    protected final Logger LOG = LoggerFactory.getLogger(getClass());
    private static final long serialVersionUID = -2866930830066526910L;
    protected Rol rol;
    protected List<UIComponent> contents = new ArrayList();
    protected static UIViewRoot uiViewRoot = new UIViewRoot();

    public Rol getRol() {
        return rol;
    }



    private static final String styleActiveMenuItem = "ui-menu-state-active";
//    private static final String styleActiveMenu = "ui-icon-triangle-1-s";
//    private static final String styleUnActiveMenu = "ui-icon-triangle-1-e";
//    private static final String styleActive = "ui-state-active";
//    private static final String styleDefault = "ui-state-default";



    private MethodExpressionActionListener createMethodActionListener(String valueExpression, Class<?> valueType, Class<?>[] expectedParamTypes) {
        MethodExpressionActionListener actionListener = null;
        try {
            actionListener = new MethodExpressionActionListener(createMethodExpression(valueExpression, valueType, expectedParamTypes));
        } catch (Exception e) {
            LOG.error("error ",e);
        }
        return actionListener;
    }

    private MethodExpression createMethodExpression(String valueExpression, Class<?> valueType, Class<?>[] expectedParamTypes) {
        MethodExpression methodExpression = null;
        try {
            ExpressionFactory factory = FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
            methodExpression = factory.createMethodExpression(FacesContext.getCurrentInstance().getELContext(), valueExpression, valueType, expectedParamTypes);
        } catch (Exception e) {
          LOG.error("Error",e);
        }
        return methodExpression;
    }


    private MethodExpression createMethodExpression(String action) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        MethodExpression methodExpression = expressionFactory.createMethodExpression(facesContext.getELContext(), action, null, new Class<?>[0]);
        return methodExpression;
    }


    public List<UIComponent> getContents() {
        return contents;
    }



    public void addSeparator(Separator separator) {

    }

    protected void resetContents() {
        contents = new ArrayList<UIComponent>();
    }

}
